const Mock = require('mockjs')
const express = require('express')
const router =express.Router()
const UserModel = require('../models/user')
const mongoose = require('mongoose')
const jwt = require('jsonwebtoken')

mongoose.connect('mongodb://localhost:27017/Blog')

const secret = "kjafhkjsal";
let createToken = (data,expiresIn)=>{//创建token的方法
    let obj = {};
    obj.data = data || {};//存入token的数据
    obj.ctime = (new Date()).getTime();//token的创建时间
    obj.expiresIn = 1000*60*60*24*7//设定的过期时间
    let token = jwt.sign(obj,secret)
    return token;
}

router.post('/login',(req,res,next)=>{
  const {user_name,password} = req.body
  UserModel
    .findOne({user_name:user_name,password:password})
    .then(result => {
      if(result){
          res.json({
          code:200,
          data:{
            user:result,
            token:createToken(result)
          },
          message:'登录成功',
        })
      }
      else{
          res.json({
              code:400,
              message:'登录失败',
          })
      }
      
    })
})

router.post('/register',(req,res,next)=>{
  const {user_name,password,nickname,user_email,} = req.body
  UserModel.findOne({user_name:user_name}).then(result =>{
    if(result){
      res.json({
        code:400,
        message:'用户名已存在',
      })
    }
    else{
      UserModel.find().then(result=>{
        let data = Mock.mock({
          user_id:result.length?result[result.length-1].user_id+1:0,
          user_name,
          password,
          nickname,
          user_email,
          user_photo:'@image(64x64,@color)',
          user_register_time:new Date(),
          role:'user'
        })
        data.user_photo = data.user_photo.replace('http','https')
        const session = new UserModel(data)
        session.save().then(result => {
              res.json({
                  code:200,
                  data:{
                      result
                  },
                  message:'完成注册',
              })
          })
      })
    }
  })
})

router.get('/find',(req,res,next)=>{
  UserModel
    .find()
    .then(result => {
      if(result){
          res.json({
          code:200,
          data:result,
          message:'查询成功',
        })
      }
      else{
          res.json({
              code:400,
              message:'查询失败',
          })
      }
    })
})



router.post('/update',(req,res,next)=>{
  const {user_id,user_name,password,nickname,user_email,role} = req.body

  UserModel.updateOne({user_id},{$set:{"user_name":user_name,"password":password,"nickname":nickname,"user_email":user_email,'role':role}}).then(result=>{
        res.json({
            code:200,
            data:{
                result
            },
            message:'修改完成',
        })
    })
})

router.delete('/delete',(req,res,next)=>{
  const {user_id} = req.body

  UserModel.deleteOne({user_id}).then(result=>{
      res.json({
          code:200,
          data:{
              result
          },
          message:'删除完成',
      })
  })
})
module.exports = router